package com.lxq.rental.property.service.impl;

/**
 * @description:
 * @author: LuoXiaoQiang
 * @date: 5/10/2025 1:15 AM
 * Copyright (c) 2025, LXQ Cloud All Rights Reserved.
 */

import com.lxq.rental.property.dto.PropertyDTO;
import com.lxq.rental.property.model.Property;
import com.lxq.rental.property.mapper.PropertyMapper;
import com.lxq.rental.property.service.PropertyService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PropertyServiceImpl implements PropertyService {

    private final PropertyMapper propertyMapper;

    public PropertyServiceImpl(PropertyMapper propertyMapper) {
        this.propertyMapper = propertyMapper;
    }

    @Override
    public List<Property> getAllProperties() {
        return propertyMapper.findAll();
    }

    @Override
    public Property getPropertyById(Long id) {
        return propertyMapper.findById(id);
    }

    @Override
    public Property createProperty(PropertyDTO dto) {
        Property property = new Property();
        property.setName(dto.getName());
        property.setAddress(dto.getAddress());
        property.setFloors(dto.getFloors());
        property.setRoomsPerFloor(dto.getRoomsPerFloor());
        propertyMapper.insert(property);
        return property;
    }

    @Override
    public Property updateProperty(Long id, PropertyDTO dto) {
        Property property = getPropertyById(id);
        property.setName(dto.getName());
        property.setAddress(dto.getAddress());
        property.setFloors(dto.getFloors());
        property.setRoomsPerFloor(dto.getRoomsPerFloor());
        propertyMapper.update(property);
        return property;
    }

    @Override
    public void deleteProperty(Long id) {
        propertyMapper.deleteById(id);
    }
}